<template>
  <div class="article_detail_container">
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <div v-if="Object.keys(article).length === 0">
      <van-loading  color="#1989fa">文章加载中...</van-loading>
    </div>

    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ article.title }}</h1>

        <!-- 用户信息 -->
        <van-cell center :title="article.aut_name" :label="dateFormat(article.pubdate)">
          <template #icon>
            <img :src="article.aut_photo" alt="" class="avatar">
          </template>
          <template #default>
            <div>
              <van-button type="info" size="mini" v-if="article.is_followed" @click="followedFn(true)">已关注</van-button>
              <van-button icon="plus" type="info" size="mini" plain v-else @click="followedFn(false)">关注</van-button>
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="article.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button icon="good-job" type="danger" size="small" v-if="article.attitude === 1" @click="loveFn(true)">已点赞
          </van-button>
          <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="loveFn(false)">点赞</van-button>
        </div>
      </div>
      <!-- 文章评论列表 -->
      <div>
        <CommentList></CommentList>
      </div>
    </div>
  </div>
</template>

<script>
import { getArticleDetailAPI, followUserAPI, unFollowUserAPI, loveArticleAPI, noLoveArticleAPI } from '@/api'
import { timeAgo } from '@/utils/date'
import { Notify } from 'vant'
import CommentList from './CommentList'

export default {
  name: 'MyArticleDetail',
  data () {
    return {
      article: {}
    }
  },
  components: {
    CommentList
  },
  methods: {
    dateFormat: timeAgo,
    // 1.获取文章详情
    async getArticleDetail () {
      const { data: res } = await getArticleDetailAPI(this.$route.query.artId)
      if (res.message === 'OK') {
        this.article = res.data
      }
    },

    // 2.取关/关注
    async followedFn (bool) {
      // 已关注
      if (bool) {
        // 进行取关
        this.article.is_followed = false
        await unFollowUserAPI({ autId: this.article.aut_id })
        Notify({ type: 'danger', message: '取消关注' })
      } else {
        // 进行关注
        this.article.is_followed = true
        const { data: res } = await followUserAPI({ autId: this.article.aut_id })
        if (res.message === 'OK') {
          Notify({ type: 'success', message: '关注成功' })
        }
      }
    },

    // 3.点赞/取消点赞
    async loveFn (bool) {
      // 已点赞
      if (bool) {
        // 取消点赞
        this.article.attitude = 0
        await noLoveArticleAPI({ artId: this.article.art_id })
        Notify({ type: 'danger', message: '取消点赞' })
      } else {
        // 进行点赞
        this.article.attitude = 1
        const { data: res } = await loveArticleAPI({ artId: this.article.art_id })
        if (res.message === 'OK') {
          Notify({ type: 'success', message: '点赞成功' })
        }
      }
    }
  },
  created () {
    this.getArticleDetail()
  }

}
</script>

<style scoped lang="less">
.article_detail_container {
  padding-top: 46px;
}

.article-container {
  padding: 10px;
}

.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;

  /deep/ img {
    width: 100%;
  }

  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;

  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

/deep/ .van-icon-arrow-left {
  color: #fff5ff;
}

.van-loading {
  text-align: center;
}
</style>
